<?php

namespace app\models;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
use Yii;

/**
 * This is the model class for table "user".
 *
 * @property int $user_id
 * @property string $user_name 用户
 * @property string $password 密码
 * @property string $user_hash 用户的hash列
 * @property string $mobile 手机
 * @property string $wallet_addr 钱包地址
 * @property int $status 0：未激活 1：激活 默认：0未激活
 * @property string $balance 余额
 * @property string $freeze 冻结余额
 * @property string $reg_time 注册时间
 * @property string $fur_amout 皮毛数
 * @property string $freeze_fur 冻结皮毛数
 * @property string $active_amount 激活金额
 */
class User  extends ActiveRecord implements IdentityInterface
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['balance', 'freeze', 'fur_amout', 'freeze_fur', 'active_amount'], 'number'],
            [['reg_time'], 'safe'],
            [['user_name', 'password_hash'], 'string', 'max' => 128],
            [['user_hash', 'wallet_addr'], 'string', 'max' => 128],
            [['mobile'], 'string', 'max' => 11],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'user_id' => 'User ID',
            'user_name' => '用户',
            'password_hash' => '密码',
            'user_hash' => '用户的hash列',
            'mobile' => '手机',
            'wallet_addr' => '钱包地址',
            'status' => '0：未激活 1：激活 默认：0未激活',
            'balance' => '余额',
            'freeze' => '冻结余额',
            'reg_time' => '注册时间',
            'fur_amout' => '皮毛数',
            'freeze_fur' => '冻结皮毛数',
            'active_amount' => '激活金额',
            'updated_a' => '修改金额'
        ];
    }
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }
    /**
     * Generates password hash from password and sets it to the model
     *
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
    }

    /**
     * Generates "remember me" authentication key
     */
    public function generateAuthKey()
    {
        $this->auth_key = Yii::$app->security->generateRandomString();
    }

    /**
     * @author lizhi7  <lizhi7@xin.com>
     */
    public function setUserHash()
    {
        $this->user_hash = hash('tiger128,4', $this->mobile.'abcdef'.date('H:i:s'));
    }

    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return static::findOne(['user_id' => $id]);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }

    /**
     * Finds user by username
     *
     * @param string $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        return static::findOne(['mobile' => $username]);
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->user_id;
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->auth_key;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return $this->getAuthKey() === $authKey;
    }

    /**
     * Validates password
     *
     * @param string $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password)
    {
        $password = (string) $password;
        return Yii::$app->security->validatePassword($password, $this->password_hash);
    }

    /**
     * Generates new password reset token
     */
    public function generatePasswordResetToken()
    {
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
    }

    /**
     * Removes password reset token
     */
    public function removePasswordResetToken()
    {
        $this->password_reset_token = null;
    }

    public function setActiveAmount()
    {
        $this->active_amount = bcdiv(rand(501, 800), 1000, 3);
    }

    /**
     * 扣减金额
     * @param $userId
     * @param $balance
     * @return bool
     */
    public function reduceBlance($userId, $balance)
    {
        $User = new User();
        $userRow = $User->findIdentity($userId);
        $userRow->balance = $userRow->balance - $balance;//扣除余额
        return $userRow->save();
    }

    /**
     * 增加金额
     * @param $userId
     * @param $balance
     * @return bool
     */
    public function increaseBlance($userId, $balance)
    {
        $User = new User();
        $userRow = $User->findIdentity($userId);
        $userRow->balance = $userRow->balance + $balance;//扣除余额
        return $userRow->save();
    }
}
